<!--用车申请页-->
<template>
  <div style="height: 6vh;background-color:#fff;padding:10px 20px;">
    <span style="line-height:60px;font-size:20px;" @click="addApplicationDialogVisible"
          :disabled="user.parenId==null">申请单列表</span>
    <el-button type="primary" style="float:right;margin-top:13px;" @click="opendialog" :disabled="user.parentId==null">申请用车</el-button>
  </div>
  <!-- 申请用车弹窗 -->
  <el-dialog title="创建申请单" v-model="addApplicationDialogVisible" style="width:1000px;padding:40px;"
  :before-close="handleClose" >
    <el-form label-width="80px" label-position="top">
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="用车人">
            <el-input readonly :value="user.username"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="使用时间">
            <el-date-picker
                type="datetimerange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                value-format="YYYY-MM-DD HH:mm:ss"
                format="YYYY-MM-DD HH:mm:ss"
                v-model="times"
            ></el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="车辆出发地">
            <el-input placeholder="请输入" v-model="addForm.departureAddr"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="车辆目的地" >
            <el-input placeholder="请输入" v-model="addForm.destinationAddr"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="用车事由">
            <el-input placeholder="请输入" type="textarea" resize="none" :rows="3" v-model="addForm.reason"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="备注">
            <el-input placeholder="请输入" type="textarea" resize="none" :rows="3" v-model="addForm.remark"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="驾照">
            <el-upload
                v-model:file-list="fileList"
                v-model:action="actionUrl"
                name="file"
                limit="1"
                list-type="picture-card"
                :on-preview="handlePictureCardPreview"
                :on-remove="handleRemove"
            >
              <el-icon>
                <Plus/>
              </el-icon>
            </el-upload>
            <el-dialog v-model="dialogVisible">
              <img w-full :src="dialogImageUrl"/>
            </el-dialog>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="审批人">
            <el-row :gutter="10">
              <el-col :span="12" v-if="auditUserOpts.length>0">
                <el-tag type="primary">
                  <el-icon style="margin-right:5px;position:relative;top:2px;">
                    <User/>
                  </el-icon>
                  <span>{{ auditUserOpts[0].username}}</span>
                </el-tag>
              </el-col>
              <el-col :span="12" v-if="auditUserOpts.length>1">
                <el-tag type="primary" >
                  <el-icon style="margin-right:5px;position:relative;top:2px;">
                    <User/>
                  </el-icon>
                  <span>{{ auditUserOpts[1].username}}</span>
                </el-tag>
              </el-col>
            </el-row>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="addApplication">确定</el-button>
    </template>
  </el-dialog>

  <!-- 用车申请列表 搜索卡片 -->
  <el-card style="margin: 20px;">
    <el-form style="padding-top:10px;">
      <el-row :gutter="30">
        <el-col :span="5">
          <el-form-item label="出发地">
            <el-input placeholder="请输入出发地" v-model="searchApplication.departureAddr" @keydown.enter="loadApplication"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item label="目的地">
            <el-input placeholder="请输入目的地" v-model="searchApplication.destinationAddr"  @keydown.enter="loadApplication"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item label="申请单状态" >
            <el-select placeholder="请选择" v-model="searchApplication.status" @change="loadApplication">
<!--              <el-option label="已发起" value="10"></el-option>-->
              <el-option v-for="item in appOptions" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item>
            <el-button @click="resetSearch">重置</el-button>
            <el-button type="primary" @click="loadApplication">查询</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </el-card>
  <!-- 用车申请表格 -->
  <el-card style="margin:20px">
    <el-table :data="tableData">
      <el-table-column type="index" width="80" align="center" prop="id" label="编号"></el-table-column>
      <el-table-column align="center" prop="username" label="申请人"></el-table-column>
      <el-table-column align="center" prop="departureAddr" label="出发地"></el-table-column>
      <el-table-column align="center" prop="destinationAddr" label="目的地"></el-table-column>
      <el-table-column align="center" prop="reason" label="用车原因"></el-table-column>
      <el-table-column align="center" prop="auditUsernameList" label="审批人"></el-table-column>
      <el-table-column align="center" prop="startTime" label="使用开始时间"></el-table-column>
      <el-table-column align="center" prop="endTime" label="使用结束时间"></el-table-column>
      <el-table-column align="center" prop="status" label="申请单状态" :formatter="appStatusFormatter" ></el-table-column>
      <el-table-column label="操作" width="120" align="center">
        <template #default="scope">
          <el-button type="primary" size="small" link
          :disabled="scope.row.status!=10" @click="cancel(scope.row.id)">撤销</el-button>
        </template>
      </el-table-column>
    </el-table>

  </el-card>
</template>
<script setup>
import {onMounted, ref} from "vue";
import axios from "axios";
import {ElMessage} from "element-plus";
import qs from "qs";
//定义变量控制创建申请单弹窗是否出现
const addApplicationDialogVisible = ref(false);

//获取当前用户
const user = ref(getUser())
/**图片上传相关代码开始**/
const fileList = ref([]);
const dialogImageUrl = ref('');
const dialogVisible = ref(false);
//定义变量用来保存图片上传给哪个后端接口地址
const actionUrl = ref(BASE_URL + '/v1/file/upload');
//定义方法点击申请用车打开弹窗
const opendialog=()=>{
  addApplicationDialogVisible.value=true;
}
//定义数组来保存新增申请单时的多个审批人对象
const auditUserOpts=ref([]);
//定义数组来保存申请单对应的审批人id集合
const auditUserIdList=ref([]);
//定义加载审批人的方法
const loadAuditUser=()=>{
  //获取当前登录人的直属领导
  let parentId =user.value.parentId;
  //根据此直属领导id查出对应的领导数据
  axios.get(BASE_URL+'/v1/user/select/audit/'+parentId).then((response)=>{
    if (response.data.code==2000){
      auditUserOpts.value=response.data.data;
      //遍历上方的审批人数组，以此取出当前遍历的审批人id，存入上方的审批人id数组
      for (let i=0;i<auditUserOpts.value.length;i++){
        //auditUserIdList里面存的id用来指定审批人，向谁发送审批单，按照次序
        auditUserIdList.value.push(auditUserOpts.value[i].id)
      }
      console.log(auditUserOpts.value)
      console.log(auditUserIdList.value)
    }else{
      ElMessage.error(response.data.msg)
    }
  })
}
onMounted(()=>{
  loadAuditUser()
})

//移除图片
const handleRemove = (uploadFile, uploadFiles) => {
  console.log(uploadFile, uploadFiles);
  //1.得到要删除的图片路径 uploadFile就是我们要删除的那个图片文件对象
  let imgUrl = uploadFile.response.data;
  //2.给后端发请求,删除图片
  axios.post(BASE_URL + '/v1/file/remove?imgUrl=' + imgUrl)
      .then((response) => {
        if (response.data.code == 2000) {
          ElMessage.success('删除成功!');
        }else{
          ElMessage.error(response.data.msg);
        }
      })
}
//图片上传后预览
const handlePictureCardPreview = (uploadFile) => {
  dialogImageUrl.value = uploadFile.url
  dialogVisible.value = true;
  console.log(uploadFile);
  //console.log(fileList.value[0].response.data);
}
/**图片上传相关代码结束**/
//定义数组用来保存表单中日期空间的数据
const times=ref([])
//定义对象来保存表单中的普通数据
const addForm =ref({
  destinationAddr:'',
  departureAddr:"",
  reason:'',
  remark:''
})
//定义新增申请得到方法
const addApplication =()=>{
  if (fileList.value.length==0){
    ElMessage.error('未上传图片！')
    return;
  }
  addForm.value.userId=user.value.id;
  addForm.value.username=user.value.username;
  addForm.value.startTime=times.value[0];
  addForm.value.endYime=times.value[1];
  addForm.value.imgUrl = fileList.value[0].response.data;
  addForm.value.auditUserIdList=auditUserIdList.value;
  console.log(addForm.value);

  let data = qs.stringify(addForm.value);
  axios.post(BASE_URL+'/v1/application/save',data).then((response)=>{
    if (response.data.code==2000){
      ElMessage.success('申请成功！');
      times.value=[];//清空时间选择器
      fileList.value=[];//清空图片上传组件
      addForm.value={};//清空form中其他项
      addApplicationDialogVisible.value=false;
      loadApplication();
    }else{
      ElMessage.error(response.data.msg)
    }
  })
}


//定义对象用来保存申请单搜索对象
const searchApplication=ref({
  departureAddr:'',
  destinationAddr:'',
  status:''
})
//定义数组对象来爆粗沈沁但表格数据
const tableData=ref([])
//加载申请单列表的方法
const loadApplication=()=>{
  let data=qs.stringify(searchApplication.value)
  axios.get(BASE_URL+'/v1/application/select',data).then((response)=>{
    if (response.data.code==2000){
      tableData.value=response.data.data;
      console.log("申请单列表="+tableData.value)
    }else {
      ElMessage.error(response.data.msg)
    }
  })

}
onMounted(()=>{
  loadApplication();
})
//重置的方法
const resetSearch=()=>{
  searchApplication.value={}
  loadApplication()
}

//定义一个数组用来保存查出来的多个字典项数据
const appOptions = ref([]);
onMounted(()=>{
  loadDictOption(appOptions,'application_status')
})
//定义申请单表格中,申请单状态列的文字转换 10=>已发起
const appStatusFormatter=(row, column, cellValue, index)=>{
  if(!appOptions.value){
    return cellValue;
  }
  for(let item of appOptions.value){
    if(item.value == cellValue){
      cellValue = item.label;
      return cellValue;
    }
  }
}
// const appStatusFormatter = (row, column, cellValue, index) => {
//   valueToLabelFormat(appStatusFormatter)
//   return cellValue;
// }
//定义撤销申请申请的方法
const cancel=(id)=>{
  axios.post(BASE_URL+'/v1/application/cancel/'+id).then((response)=>{
    if (response.data.code==2000){
      ElMessage.success('撤销成功')
      loadApplication();
    }else{
      ElMessage.error(response.data.msg)
    }
  })
}
const handleClose=()=>{
  if (confirm("你确定关闭弹窗吗？")){
    addForm.value={}
    times.value=[];
    fileList.value=[];
    addApplicationDialogVisible.value=false;
  }
}

</script>

<style scoped>
</style>